﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Reflection;
using System.IO;
using BaseDll;
namespace ModuleUnit
{
    public partial class Form_ModuleManage : UIPage
    {
        Type[] type_All = null;
        Dictionary<string, Type> dic_Type_Module = new Dictionary<string, Type>();

        public Form_ModuleManage()
        {
            string strDllPath = AppDomain.CurrentDomain.BaseDirectory + "\\ModuleUnit.dll";
            if (File.Exists(strDllPath))
            {
                Assembly assembly = Assembly.LoadFile(strDllPath);
                type_All = assembly.GetTypes();

            }
            foreach (Type t in type_All)
            {
                var tem = t.BaseType;
                if (tem != null)
                {
                    if (tem.Name == typeof(ModuleBase).Name)
                    {
                        var rep = t.GetCustomAttributes(typeof(DescriptionAttribute));
                        var rep2 = rep.ToList<Attribute>();
                        string strName = rep2.Count > 0 ? ((DescriptionAttribute)rep2[0]).Description : tem.FullName;
                        dic_Type_Module.Add(strName, t);
                    }
                }
            }

            InitializeComponent();
        }
        private void Function_AddTage(string strName, ModuleBase Module)
        {
            dg_ModuleList.Rows.Add(strName, Module.strType);
        }

        private void Form_ModuleManage_Load(object sender, EventArgs e)
        {
            try
            {
                dg_ModuleList.Rows.Clear();
                foreach (var item in ModuleMgr.GetInstance().dic_ModuleInfo)
                {
                    Function_AddTage(item.Key, item.Value);
                }
                List<string> listType = new List<string>();
                cbb_ModuleType.Items.Clear();
                foreach (var item in dic_Type_Module)
                {
                    cbb_ModuleType.Items.Add(item.Key);
                }
                cbb_ModuleType.DropDownStyle = UIDropDownStyle.DropDownList;
                if (cbb_ModuleType.Items.Count >= 0)
                    cbb_ModuleType.SelectedIndex = 0;
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_AddModule_Click(object sender, EventArgs e)
        {
            try
            {
                string str_ModuleName = txt_ModuleName.Text;
                string str_Description = cbb_ModuleType.Text;
                if (ModuleMgr.GetInstance().dic_ModuleInfo.ContainsKey(str_ModuleName))
                {
                    MessageBox.Show($"已存在{str_ModuleName}模块了，请勿重复添加");
                    return;
                }
                Type type = AssemblyOperate.GetTypeByDescription(str_Description, type_All.ToList());
                string strTypeName = type.FullName;
                Type Test = Type.GetType(strTypeName);
                object[] args = new object[] { null, str_Description };
                ModuleBase module = Activator.CreateInstance(Test, args) as ModuleBase;
                module.strType = strTypeName;
                ModuleMgr.GetInstance().dic_ModuleInfo.Add(str_ModuleName, module);
                Function_AddTage(str_ModuleName, module);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }
        private void dg_ModuleList_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dg_ModuleList_SelectIndexChange(object sender, int index)
        {
            try
            {
                int n_Index = index;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的模组"); return;
                }
                else
                {
                    string strName = dg_ModuleList.Rows[n_Index].Cells[0].Value.ToString();
                    ModuleBase module = ModuleMgr.GetInstance().Function_获取指定模块(strName);
                    propertyGrid_ModuleInfo.SelectedObject = module.obj_Config;
                    propertyGrid_ModuleInfo.Tag = strName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void propertyGrid_ModuleInfo_Click(object sender, EventArgs e)
        {

        }

        private void btn_SaveParamter_Click_1(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_ModuleList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else
                {
                    string strDeviceName = dg_ModuleList.Rows[n_Index].Cells[0].Value.ToString();
                    ModuleMgr.GetInstance().dic_ModuleInfo[strDeviceName].obj_Config = propertyGrid_ModuleInfo.SelectedObject;
                    ModuleMgr.GetInstance().Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void 上移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_ModuleList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else if (n_Index == 0)
                {
                    MessageBox.Show("第一行无法上移"); return;

                }
                else
                {
                    List<string> listValue = new List<string>();
                    for (int i = 0; i < dg_ModuleList.ColumnCount; i++)
                    {
                        listValue.Add(dg_ModuleList.Rows[n_Index].Cells[i].Value.ToString());
                    }
                    for (int i = 0; i < dg_ModuleList.ColumnCount; i++)
                    {
                        dg_ModuleList.Rows[n_Index].Cells[i].Value = dg_ModuleList.Rows[n_Index - 1].Cells[i].Value;
                        dg_ModuleList.Rows[n_Index - 1].Cells[i].Value = listValue[i];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void 下移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_ModuleList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的设备"); return;
                }
                else if (n_Index == dg_ModuleList.RowCount - 1)
                {
                    MessageBox.Show("第一行无法下移"); return;

                }
                else
                {
                    List<string> listValue = new List<string>();
                    for (int i = 0; i < dg_ModuleList.ColumnCount; i++)
                    {
                        listValue.Add(dg_ModuleList.Rows[n_Index].Cells[i].Value.ToString());
                    }
                    for (int i = 0; i < dg_ModuleList.ColumnCount; i++)
                    {
                        dg_ModuleList.Rows[n_Index].Cells[i].Value = dg_ModuleList.Rows[n_Index + 1].Cells[i].Value;
                        dg_ModuleList.Rows[n_Index + 1].Cells[i].Value = listValue[i];
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int n_Index = dg_ModuleList.SelectedIndex;
                if (n_Index < 0)
                {
                    MessageBox.Show("请先选中要操作的模块"); return;
                }
                else
                {
                    if (UIMessageDialog.ShowAskDialog(this, "是否删除所选？"))
                    {
                        string strName = dg_ModuleList.Rows[n_Index].Cells[0].Value.ToString();
                        ModuleMgr.GetInstance().dic_ModuleInfo.Remove(strName);
                        dg_ModuleList.Rows.RemoveAt(n_Index);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (UIMessageDialog.ShowAskDialog(this, "是否保存当前列表顺序？"))
                {
                    Dictionary<string, ModuleBase> dic_DeviceInfo = new Dictionary<string, ModuleBase>();
                    for (int i = 0; i < dg_ModuleList.RowCount; i++)
                    {
                        string strName = dg_ModuleList.Rows[i].Cells[0].Value.ToString();
                        dic_DeviceInfo.Add(strName, ModuleMgr.GetInstance().dic_ModuleInfo[strName]);
                    }
                    ModuleMgr.GetInstance().dic_ModuleInfo = dic_DeviceInfo;
                    ModuleMgr.GetInstance().Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
